package com.peaksport.pkms.basedata.master.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.peaksport.framework.base.dao.IBaseDAO;
import com.peaksport.framework.base.service.CoreBaseService;
import com.peaksport.framework.exception.PKDevException;
import com.peaksport.pkms.basedata.master.entity.BaseOrgEntity;
import com.peaksport.pkms.basedata.master.venum.OrgType;

@Component
public class OrgDelegate extends CoreBaseService {
	
	/**
	 * 根据源组织的委托关系获取目标组织列表
	 * @param fromOrgId 源组织Id
	 * @param fromOrgType 源组织类型
	 * @param toOrgType 目标组织类型
	 * @Return 委托目标组织列表
	 */
	public List<BaseOrgEntity> getOrgByRelation(String fromOrgId, OrgType fromOrgType, OrgType toOrgType) {
		return getOrgByRelation(fromOrgId, fromOrgType, toOrgType,false);
	}
	
	/**
	 * 根据源组织的委托关系获取目标组织列表
	 * @param fromOrgId 源组织Id
	 * @param fromOrgType 源组织类型
	 * @param toOrgType 目标组织类型
	 * @param isDefault 返回默认或按组织编码排序的第一个
	 * @Return 委托目标组织列表
	 */
	public List<BaseOrgEntity> getOrgByRelation(String fromOrgId, OrgType fromOrgType, OrgType toOrgType, boolean isDefault) {
		StringBuffer sql = new StringBuffer();
	    sql.append("select org.FID,org.FNumber,org.FName_l2")
	    .append("from t_org_unitrelation ur ")
	    .append("left join t_org_baseunit org on org.FID=ur.ftounitid ")
	    .append("where org.FIsOUSealup = 0 ")
	    .append("and ur.ffromunitid='").append(fromOrgId).append("' ")
	    .append("and ur.ftyperelationid=(select fid from t_org_typerelation where ffromtype=").append(fromOrgType.getValue()).append(" and ftotype=").append(toOrgType.getValue()).append(")")
	    .append("order by ur.FIsDefault desc,org.FNumber ");
	    List<BaseOrgEntity> lstOrg = (List<BaseOrgEntity>) queryRecordForNative(sql.toString(), BaseOrgEntity.class);
	    if (isDefault && !lstOrg.isEmpty()) {
	    	List<BaseOrgEntity> defaultOrg = new ArrayList<>();
	    	defaultOrg.add(lstOrg.get(0));
	    	return defaultOrg;
	    }
		return lstOrg;
	}
	
	/**
	 * 根据目标组织的被委托关系获取源组织列表
	 * @param toOrgId 目标组织Id
	 * @param toOrgType 目标组织类型
	 * @param fromOrgType 源组织类型
	 * @return 被委托源组织列表
	 */
	public List<BaseOrgEntity> getOrgByReverseRelation(String toOrgId, OrgType toOrgType, OrgType fromOrgType) {
		return getOrgByReverseRelation(toOrgId, toOrgType, fromOrgType, false);
	}
	
	/**
	 * 根据目标组织的被委托关系获取源组织列表
	 * @param toOrgId 目标组织Id
	 * @param toOrgType 目标组织类型
	 * @param fromOrgType 源组织类型
	 * @param isDefault 返回默认或按组织编码排序的第一个
	 * @return 被委托源组织列表
	 */
	public List<BaseOrgEntity> getOrgByReverseRelation(String toOrgId, OrgType toOrgType, OrgType fromOrgType, boolean isDefault) {
		StringBuffer sql = new StringBuffer();
	    sql.append("select org.FID,org.FNumber,org.FName_l2")
	    .append("from t_org_unitrelation ur ")
	    .append("left join t_org_baseunit org on org.FID=ur.ftounitid ")
	    .append("where org.FIsOUSealup = 0 ")
	    .append("and ur.FToUnitId='").append(toOrgId).append("' ")
	    .append("and ur.ftyperelationid=(select fid from t_org_typerelation where ffromtype=").append(fromOrgType.getValue()).append(" and ftotype=").append(toOrgType.getValue()).append(")")
	    .append("order by ur.FIsDefault desc,org.FNumber ");
	    List<BaseOrgEntity> lstOrg = (List<BaseOrgEntity>) queryRecordForNative(sql.toString(), BaseOrgEntity.class);
	    if (isDefault && !lstOrg.isEmpty()) {
	    	List<BaseOrgEntity> defaultOrg = new ArrayList<>();
	    	defaultOrg.add(lstOrg.get(0));
	    	return defaultOrg;
	    }
		return lstOrg;
	}
	
	/**
	 * 根据上级组织获取其所有下级组织列表
	 * @param parentOrgId 上级组织ID
	 * @param orgType 组织类型
	 * @return
	 */
	public List<BaseOrgEntity> getAllSubOrg(String parentOrgId, OrgType orgType) {
		return getSubOrg(parentOrgId, orgType, true);
	}
	
	/**
	 * 根据上级组织获取其下级组织列表
	 * @param parentOrgId 上级组织ID
	 * @param orgType 组织类型
	 * @param includeAllSub  包含所有下级
	 * @return
	 */
	public List<BaseOrgEntity> getSubOrg(String parentOrgId, OrgType orgType, boolean includeAllSub) {
		verifyDevNull(parentOrgId, "上级组织ID不能为空");
		verifyDevNull(orgType, "组织类型不能为空");
		
		String orgTableName = "";
		switch (orgType) {
			case Admin: orgTableName = "t_org_admin"; break;
			case Company: orgTableName = "t_org_company"; break;
			case Sale: orgTableName = "t_org_sale"; break;
			case Purchase:  orgTableName = "t_org_purchase"; break;	
			case Storage:  orgTableName = "t_org_storage"; break;	
			default: throw PKDevException.throwException("暂不支持改组织类型[%s]", orgType.getClass().getName());
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append("select FID,FName_l2,FNumber from ").append(orgTableName).append(" ");
		if (includeAllSub) {
			sql.append("start with FID='").append(parentOrgId).append("' connect by prior FID = FParentID ");
		} else {
			sql.append("where FParentId='").append(parentOrgId).append("'");
		}
		List<BaseOrgEntity> lstOrg = (List<BaseOrgEntity>) queryRecordForNative(sql.toString(), BaseOrgEntity.class);
		return lstOrg;
	}

	@Override
	public IBaseDAO<?> getDAO() {
		return null;
	}

}
